/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.common.util;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 *
 *@author joewee
 *@version 1.0.0
 *@Date 2018/2/23 15:57
 */
public class HttpRequestUtils {

    private static final String FTP_IMAGE_URL_PREFIX = "http://img.sjgo365.com/cbest/bh/wxmall/";

    private static final List<String> DOMAIN_ROOT_PATH = Lists.newArrayList("http://192.1.34.31:8080", "http://192.1.34.31:8000","http://192.1.34.32:8080", "http://192.1.34.32:8000");

    public static String getApplicationContext(HttpServletRequest request){
       return StringUtils.remove(request.getRequestURL().toString(), request.getRequestURI());
    }

    /**
     * 获取当前线程 Request请求
     * @return
     */
    public static String getUrlAccessPath(){
        return getUrlAccessPath(getCurrentRequest());
    }

    /**
     * 获取request的Url网络访问路径
     * @param request
     * @return
     */
    public static String getUrlAccessPath(HttpServletRequest request){
        if (StringUtils.isBlank(request.getContextPath())){
            return getApplicationContext(request);
        }
        return getApplicationContext(request).concat(request.getContextPath());
    }

    /**
     *  获取当前线程 request请求
     * @return
     */
    public static HttpServletRequest getCurrentRequest(){
        ServletRequestAttributes attributes = null;
        Assert.notNull(attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(),"当前线程没有 request..");
        return attributes.getRequest();
    }

    /**
     * 获取图片的绝对路径
     * @param url
     * @return
     */
    /**
     * 获取图片的绝对路径
     * @param url
     * @return
     */
    public static String getUrl(String url){
        String domain = "http://weixin.e-cbest.com";
        if(url==null) return null;
        if(url.startsWith("http://")||url.startsWith("HTTP://")){
            for (String rootUrl : DOMAIN_ROOT_PATH) {
                if (url.startsWith(rootUrl)){
                    return url.replaceFirst(rootUrl,domain);
                }
            }
            return url;
        }else {
            return FTP_IMAGE_URL_PREFIX.concat(removeHeadSlash(url));
        }
    }

    private static String removeHeadSlash(String url) {
        while(url.startsWith("/"))
            url = url.substring(1,url.length());
        return url;
    }


}
